<template>
  <div class="comment">
    <h4>评论</h4>
    <!-- 评论窗口 -->
    <div class="comment-box">
      <div class="comment-avatar">
        <img :src="avatar" alt="" />
      </div>
      <div class="comment-right">
        <div
          class="comment-textarea"
          contenteditable="true"
          ref="textAreaRef"
          @input="handleInputChange"
          placeholder="说点什么呢"
        ></div>
        <div class="bottom-btn">
          <svg-icon name="emoil" @click="handleChangeEmoji"></svg-icon>
          <el-button type="danger" @click="handleOnSubmit">发表</el-button>
        </div>
        <div class="emoji-wrap" v-if="showEmoji">
          <div class="emoji">
            <Emoji @changeEmoji="handleSelectEmoji" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import {computed,ref} from 'vue'
import { useStore } from 'vuex';
import {useRoute} from "vue-router"
const $emits = defineEmits(['change']);
let showEmoji = ref(false);
const $store = useStore();
const textAreaRef = ref();
const Route = useRoute();
import {addComment} from "@/api/article.js"
// 计算属性
const avatar = computed(() => {
  return $store.state.user.userInfo.avatar;
});


const handleChangeEmoji = () => {
  showEmoji.value = !showEmoji.value;
};

const handleInputChange = (e) => {
  let value = e.target.innerHTML;
  if (value) {
    e.target.classList.add("has-content");
  } else {
    e.target.classList.remove("has-content");
  }
};

const handleSelectEmoji = (url) => {
  console.log(url, "--url--");
  handleChangeEmoji();
  createImg(url);
};


const createImg = (url) => {
  const img = document.createElement("img");
  img.src = url;
  textAreaRef.value.appendChild(img);
  // 创建一个新的 Range 对象
  const range = new Range();
  // 将 Range 对象设置为 textAreaRef 的末尾
  range.selectNodeContents(textAreaRef.value);
  range.collapse(false); //是否全部选中
  // 获取当前的 Selection 对象
  const selection = window.getSelection();
  // 清除之前的选区
  selection.removeAllRanges();
  // 将新的 Range 对象添加到选区中
  selection.addRange(range);
  if (textAreaRef.value.innerHTML !== "") {
    textAreaRef.value.classList.add("has-content");
  } else {
    textAreaRef.value.classList.remove("has-content");
  }
};


const handleOnSubmit = async () => {
  const requestObj = {};
  let content = textAreaRef.value.innerHTML;
  let userId = $store.state.user.userInfo.id;
  
  if (content) {
    requestObj.content = content;
  }
  if (userId) {
    requestObj.userId = userId;
  }
    requestObj.forumId = Route.params.id;

  requestObj.istop = 1;

  console.log(requestObj, "@@");
  await addComment(requestObj);
  textAreaRef.value.innerHTML = "";
  $emits("change");
};
</script>
<style lang="scss" scoped>
.comment {
  .comment-box {
    padding: 10px;
    margin-top: 10px;
    border: 1px solid red;
    display: flex;
    justify-content: space-between;
    .comment-avatar {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .comment-right {
      flex: 1;
      position: relative;
      .comment-textarea {
        min-height: 122px;
        position: relative;
        padding-top: 10px;
        padding-left: 10px;
        margin-left: 10px;
        // border: 1px solid cyan;
        &::after {
          font-size: 14px;
          content: attr(placeholder);
          color: #666;
          position: absolute;
          top: 10px;
        }
        &.has-content::after {
          display: none;
        }
      }
      .bottom-btn {
        align-items: center;
        margin-top: 10px;
        padding: 0 10px;
        display: flex;
        justify-content: space-between;
      }
      :deep svg {
        cursor: pointer;
      }
      .emoji-wrap {
        position: absolute;
        left: 20px;
        top: -70px;
        z-index: 10;
      }
    }
  }
  .reply-news {
    margin-top: 10px;
    .news-item {
      margin-bottom: 10px;
      &.replyClass {
        padding-left: 20px;
      }
      .btn {
        opacity: 0;
        transition: all 0.2s ease;
        &.hover {
          opacity: 1;
          // display: block;
        }
      }
      .news-user {
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        margin-bottom: 10px;
        .left {
          display: flex;
        }
      }
      .content {
        display: flex;
        align-items: center;
        background: #f5f5f5;
        padding: 10px;
      }
    }
  }
}
</style>
